package com.ys.utils.push;


import cn.jpush.api.push.PushClient;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.ys.base.BaseConstants;
import com.ys.base.EnumType;
import com.ys.utils.base.j2se.Common;
import com.ys.utils.base.j2se.StringHandler;
import com.ys.utils.help.GroupUtils;
import com.ys.utils.redis.Redis;
import com.ys.utils.redis.key.UserKey;
import com.ys.web.user.friends.po.BaseGroupInfoPO;
import com.ys.web.ws.bean.MessageRequestBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author quxinjun
 * @version 1.0.0
 * @ClassName JPushUtils.java
 * @Description TODO
 * @createTime 2021年12月24日 18:54:00
 */
public class JPushUtils {

//    private static final String appKey = "8a1f5cb4456428cd2b9bc035";
//
//    private static final String secret = "4ff768499995803808a59804";

    /**
     * 尚品
     private static final String appKey = "ebcb9abd26a9bfe56c6bca04";

     private static final String secret = "07eb9547de031e7e9c5800cb";
     */

    private static final String appKey = "379f69b58dd16e5ad62ea570";

    private static final String secret = "f9a092680bf1dafee96e8e8b";
    private static void send(PushPayload pushPayload) {
        try {
            PushClient pushClient = new PushClient(secret, appKey);
            pushClient.sendPush(pushPayload);
        } catch (Exception e) {
//            Logger.error("推送出错，" + e.getMessage());
        }
    }


    public static void sendNotification(Set<String> member, MessageRequestBean messageRequestBean) {
        String title = null;
        String data = null;
        if (StringHandler.isNotEmpty(messageRequestBean.getGroupId())) {
            BaseGroupInfoPO cacheGroupInfo = GroupUtils.getCacheGroupInfo(messageRequestBean.getGroupId());
            title = cacheGroupInfo.getName() + "\n\n";
            data = messageRequestBean.getUserName() + ":" + getAlertData(messageRequestBean);
        } else {
            title = messageRequestBean.getUserName();
            data = getAlertData(messageRequestBean);
        }

        PushPayload pushPayload = all_notification(member, title, data, messageRequestBean.getGroupId());
        if(pushPayload != null){
            send(pushPayload);
        }
    }

    public static PushPayload all_notification(Set<String> members, String title, String alert, String groupId) {
        List<Object> list = Redis.hash.hmget(UserKey.jPushRelationIdKey(), new ArrayList<>(members));
        if(list.isEmpty()){
            return null;
        }
        List<String> collect = list.stream().map(x -> (String) x).filter(Common::isNotEmpty).collect(Collectors.toList());
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.registrationId(collect))
                .setOptions(Options.newBuilder().setApnsProduction(true).build())
                .setNotification(
                        Notification
                                .newBuilder()
                                .addPlatformNotification(
                                        AndroidNotification
                                                .newBuilder()
                                                .setTitle(title)
                                                .setAlert(alert)
                                                .setLargeIcon(BaseConstants.ClientConstants.SYS_NOTICE_HEAD_IMG)
                                                .setBadgeClass("com.ashuzhuang.cn")
                                                .setBadgeAddNum(1)
                                                .build())
                                .addPlatformNotification(
                                        IosNotification
                                                .newBuilder()
                                                .setAlert(title + (StringHandler.isNotEmpty(groupId) ? "" : ":") + alert)
                                                .setSound("default")
                                                .incrBadge(1)
                                                .build())
                                .build()
                ).build();
    }


    /**
     * 组装 安卓 通知
     *
     * @param members
     * @param title
     * @param alert
     * @return
     */
    public static PushPayload android_notification(Set<String> members, String title, String alert) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.alias(members))
                .setNotification(
                        Notification
                                .newBuilder()
                                .addPlatformNotification(
                                        AndroidNotification
                                                .newBuilder().
                                                setTitle(title)
                                                .setAlert(alert)
                                                .setSound("default")
                                                .setBigPicPath(BaseConstants.ClientConstants.SYS_NOTICE_HEAD_IMG)
                                                .setBadgeClass("com.ashuzhuang.cn")
                                                .setBadgeAddNum(1)
                                                .build()
                                ).build()
                ).build();
    }

    /**
     * 组装 ios 通知
     *
     * @param members
     * @param title
     * @param alert
     * @return
     */
    public static PushPayload ios_notification(Set<String> members, String title, String alert) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.alias(members))
                .setNotification(
                        Notification
                                .newBuilder()
                                .addPlatformNotification(
                                        IosNotification
                                                .newBuilder()
                                                .setAlert(title + alert)
                                                .setSound("default")
                                                .incrBadge(1)
                                                .build()
                                ).build()
                ).build();
    }


    private static String getAlertData(MessageRequestBean messageRequestBean) {
        if (EnumType.MessageCode.MESSAGE_SOUNDS.getCode().equals(messageRequestBean.getCode())) {
            return "[" + EnumType.MessageCode.MESSAGE_SOUNDS.getDesc() + "]\uD83D\uDD0A";
        } else if (EnumType.MessageCode.MESSAGE_IMAGE.getCode().equals(messageRequestBean.getCode())) {
            return "[" + EnumType.MessageCode.MESSAGE_IMAGE.getDesc() + "]";
        } else if (EnumType.MessageCode.MESSAGE_RED_PACK.getCode().equals(messageRequestBean.getCode())) {
            return "[" + EnumType.MessageCode.MESSAGE_RED_PACK.getDesc() + "]\uD83E\uDDE7";
        } else if (EnumType.MessageCode.MESSAGE_TRANSFER.getCode().equals(messageRequestBean.getCode())) {
            return "[" + EnumType.MessageCode.MESSAGE_TRANSFER.getDesc() + "]\uD83D\uDCB0";
        } else if (EnumType.MessageCode.MESSAGE_SEND_USER_CARD.getCode().equals(messageRequestBean.getCode())) {
            return "[" + EnumType.MessageCode.MESSAGE_SEND_USER_CARD.getDesc() + "]";
        }
        return messageRequestBean.getData();
    }
}
